package com.xxxy.ljh.myapplication.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxxy.ljh.myapplication.Adapter.MemberApplicationAdapter;
import com.xxxy.ljh.myapplication.Dao.MemberApplicationDao;
import com.xxxy.ljh.myapplication.DaoImpl.MemberApplicationDaoImpl;
import com.xxxy.ljh.myapplication.R;
import com.xxxy.ljh.myapplication.model.MemberApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * 社团成员申请审核Activity
 * 展示所有待审核的成员申请，并提供通过/拒绝功能
 */
public class MemberApplicationReviewActivity extends AppCompatActivity implements MemberApplicationAdapter.OnApplicationActionListener {
    private RecyclerView recyclerView;
    private MemberApplicationAdapter adapter;
    private List<MemberApplication> applicationList;
    private MemberApplicationDao applicationDao;
    
    // 当前社团ID（应该从Intent中获取）
    private int currentClubId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_application_review);
        
        // 从Intent获取社团ID
        currentClubId = getIntent().getIntExtra("club_id", -1);
        
        if (currentClubId == -1) {
            Toast.makeText(this, "无效的社团信息", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        initViews();
        loadApplications();
    }
    
    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView_applications);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        
        applicationList = new ArrayList<>();
        adapter = new MemberApplicationAdapter(applicationList, this);
        recyclerView.setAdapter(adapter);
        
        applicationDao = new MemberApplicationDaoImpl(this);
    }
    
    private void loadApplications() {
        applicationList.clear();
        applicationList.addAll(applicationDao.getPendingApplicationsByClubId(currentClubId));
        if (applicationList.isEmpty()) {
            Toast.makeText(this, "暂无待审核的申请", Toast.LENGTH_SHORT).show();
        }
        adapter.notifyDataSetChanged();
    }
    
    @Override
    public void onApproveApplication(MemberApplication application) {
        // 处理通过申请
        boolean success = applicationDao.approveApplication(
                application.getMemberId(), 
                1, // 通过状态
                getCurrentUserId(), // 审核人ID
                "审核通过" // 备注
        );
        
        if (success) {
            Toast.makeText(this, "申请已通过", Toast.LENGTH_SHORT).show();
            // 从列表中移除已处理的申请
            applicationList.remove(application);
            adapter.notifyDataSetChanged();
            
            // 如果列表为空，给出提示
            if (applicationList.isEmpty()) {
                Toast.makeText(this, "所有申请已处理完毕", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    @Override
    public void onRejectApplication(MemberApplication application) {
        // 处理拒绝申请
        boolean success = applicationDao.approveApplication(
                application.getMemberId(), 
                2, // 拒绝状态
                getCurrentUserId(), // 审核人ID
                "审核拒绝" // 备注
        );
        
        if (success) {
            Toast.makeText(this, "申请已拒绝", Toast.LENGTH_SHORT).show();
            // 从列表中移除已处理的申请
            applicationList.remove(application);
            adapter.notifyDataSetChanged();
            
            // 如果列表为空，给出提示
            if (applicationList.isEmpty()) {
                Toast.makeText(this, "所有申请已处理完毕", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    /**
     * 获取当前登录用户ID
     * 从SharedPreferences中获取当前登录用户ID
     */
    private int getCurrentUserId() {
        SharedPreferences prefs = getSharedPreferences("user_prefs", MODE_PRIVATE);
        return prefs.getInt("user_id", -1);
    }
}